/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.PreexistingEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.Event;
import MeteoCal.entity.User;
import MeteoCal.entity.Warningevent;
import MeteoCal.entity.WarningeventPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class WarningeventJpaController implements Serializable {

    public WarningeventJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Warningevent warningevent) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (warningevent.getWarningeventPK() == null) {
            warningevent.setWarningeventPK(new WarningeventPK());
        }
        warningevent.getWarningeventPK().setIdEvent(warningevent.getEvent().getIdEvent());
        warningevent.getWarningeventPK().setIdUser(warningevent.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Event event = warningevent.getEvent();
            if (event != null) {
                event = em.getReference(event.getClass(), event.getIdEvent());
                warningevent.setEvent(event);
            }
            User user = warningevent.getUser();
            if (user != null) {
                user = em.getReference(user.getClass(), user.getEmail());
                warningevent.setUser(user);
            }
            em.persist(warningevent);
            if (event != null) {
                event.getWarningeventCollection().add(warningevent);
                event = em.merge(event);
            }
            if (user != null) {
                user.getWarningeventCollection().add(warningevent);
                user = em.merge(user);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findWarningevent(warningevent.getWarningeventPK()) != null) {
                throw new PreexistingEntityException("Warningevent " + warningevent + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Warningevent warningevent) throws NonexistentEntityException, RollbackFailureException, Exception {
        warningevent.getWarningeventPK().setIdEvent(warningevent.getEvent().getIdEvent());
        warningevent.getWarningeventPK().setIdUser(warningevent.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Warningevent persistentWarningevent = em.find(Warningevent.class, warningevent.getWarningeventPK());
            Event eventOld = persistentWarningevent.getEvent();
            Event eventNew = warningevent.getEvent();
            User userOld = persistentWarningevent.getUser();
            User userNew = warningevent.getUser();
            if (eventNew != null) {
                eventNew = em.getReference(eventNew.getClass(), eventNew.getIdEvent());
                warningevent.setEvent(eventNew);
            }
            if (userNew != null) {
                userNew = em.getReference(userNew.getClass(), userNew.getEmail());
                warningevent.setUser(userNew);
            }
            warningevent = em.merge(warningevent);
            if (eventOld != null && !eventOld.equals(eventNew)) {
                eventOld.getWarningeventCollection().remove(warningevent);
                eventOld = em.merge(eventOld);
            }
            if (eventNew != null && !eventNew.equals(eventOld)) {
                eventNew.getWarningeventCollection().add(warningevent);
                eventNew = em.merge(eventNew);
            }
            if (userOld != null && !userOld.equals(userNew)) {
                userOld.getWarningeventCollection().remove(warningevent);
                userOld = em.merge(userOld);
            }
            if (userNew != null && !userNew.equals(userOld)) {
                userNew.getWarningeventCollection().add(warningevent);
                userNew = em.merge(userNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                WarningeventPK id = warningevent.getWarningeventPK();
                if (findWarningevent(id) == null) {
                    throw new NonexistentEntityException("The warningevent with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(WarningeventPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Warningevent warningevent;
            try {
                warningevent = em.getReference(Warningevent.class, id);
                warningevent.getWarningeventPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The warningevent with id " + id + " no longer exists.", enfe);
            }
            Event event = warningevent.getEvent();
            if (event != null) {
                event.getWarningeventCollection().remove(warningevent);
                event = em.merge(event);
            }
            User user = warningevent.getUser();
            if (user != null) {
                user.getWarningeventCollection().remove(warningevent);
                user = em.merge(user);
            }
            em.remove(warningevent);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Warningevent> findWarningeventEntities() {
        return findWarningeventEntities(true, -1, -1);
    }

    public List<Warningevent> findWarningeventEntities(int maxResults, int firstResult) {
        return findWarningeventEntities(false, maxResults, firstResult);
    }

    private List<Warningevent> findWarningeventEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Warningevent.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Warningevent findWarningevent(WarningeventPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Warningevent.class, id);
        } finally {
            em.close();
        }
    }

    public int getWarningeventCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Warningevent> rt = cq.from(Warningevent.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
